﻿using LDC_Shop_B.Models;
using LDC_Shop_B.Models.Database;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LDC_Shop_B.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        private readonly LDC_ShopBContext _db;

        public GoodsController(LDC_ShopBContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 新增商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string AddGood(Good good)
        {
            good.CreateTime = DateTime.Now;
            _db.Goods.Add(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "新增成功";
            return "新增失敗";
        }
        /// <summary>
        /// 修改商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string UpdateGood(Good good)
        {
            var model = _db.Goods.FirstOrDefault(x => x.GoodId == good.GoodId);
            if(model==null)
                return "商品不存在";

            //賦值更新
            model.Title = good.Title;
            model.Price = good.Price;
            model.Desc = good.Desc;
            model.Stock = good.Stock;
            model.Photo = good.Photo;

            _db.Goods.Update(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "修改成功";
            return "修改失敗";
        }

        /// <summary>
        /// 查詢商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<Good> GetGoodList()
        {
            return _db.Goods.ToList();
        }
        /// <summary>
        /// 刪除商品
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpPost]
        public string DeleteGood(int goodId)
        {
            var model = _db.Goods.FirstOrDefault(x => x.GoodId == goodId);
            if (model == null)
                return "商品不存在";
            _db.Goods.Remove(model);
            var row = _db.SaveChanges();
            if (row > 0)
                return "刪除成功";
            return "刪除失敗";
        }

        /// <summary>
        /// 新增分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddCategory(GoodCategory category)
        {
            _db.GoodCategories.Add(category);
            var row = _db.SaveChanges();
            if(row>0)
                return "新增成功";
            return "新增失败";
        }
        /// <summary>
        /// 修改分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateCategory(GoodCategory category)
        {
            var model = _db.GoodCategories.FirstOrDefault(x => x.CateId == category.CateId);
            if (model == null)
                return "分类不存在";
            _db.GoodCategories.Update(model);
            var row = _db.SaveChanges();
            if (row > 0)
                return "修改成功";
            return "修改失败";
        }
        /// <summary>
        /// 查询分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<GoodCategory> GetGoodCategories()
        {

            return _db.GoodCategories.ToList();
        }
        /// <summary>
        /// 刪除分类
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        [HttpPost]
        public string DeleteCategory(int cateId)
        {
            var model = _db.GoodCategories.FirstOrDefault(x => x.CateId == cateId);
            if (model == null)
                return "分类不存在";
            _db.GoodCategories.Remove(model);
           var row = _db.SaveChanges();
            if (row > 0)
                return "刪除成功";
            return "刪除失败";
        }

        //前端商品列表接口 输入：商品分类Id,搜索关键字，分页数据 输出：商品列表（商品名称，商品Id，商品图片），总数
        /// <summary>
        /// 前端商品列表接口
        /// </summary>
        [HttpPost]
        public GetGoodShowResponse GetGoodShow(GetGoodShowRequest request)
        {
            //req.cateId== Good.CateId                1                                   null

            //固定条件查询
            var good = _db.Goods.Where(x => x.CateId == request.CateId);
            //根据条件查询
            if(!string.IsNullOrEmpty(request.Keyword))
            {
                good = good.Where(x => x.Title.Contains(request.Keyword));
            }
            //查询该条件下的总数/pagesize
            var total = good.Count();
            var list = good
                // 9
                //1 0
                //2 (PageIndex-1)*PageSize
                //排序 根据时间倒叙
                .OrderByDescending(x=>x.CreateTime)
                //跳过n
                .Skip((request.PageIndex - 1) * request.PageSize)
                //取前几条
                .Take(request.PageSize)
                //形成自定义类
                .Select(x=>new GoodShowModel
                {
                    GoodId=x.GoodId,
                    Title=x.Title,
                    Photo=x.Photo
                })
                .ToList();
            //构造返回对象
            var response = new GetGoodShowResponse()
            {
                List = list,
                Total = total
            };
            return response;
        }
        /// <summary>
        /// 前端商品详情接口 输入：商品Id 输出：商品标题，图片，价格，库存，描述
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpPost]
        public Good GetGoodInfo(int goodId)
        {
            var good = _db.Goods.FirstOrDefault(x => x.GoodId == goodId);
            if (good == null)
                return null;
            return good;
        }
    }
}
